# render service to help front-end improve performance

Q = require("q")
_ = require("underscore")

jade = require "jade"
http_request = require('./util').http_request
resErr = require("./util").resErr
async = require "async"
fs = require "fs"
ds = require "../../../views/gfportalview/ds"

{series,fail} = require("../lib/utils")


http_request2 = (url,cb) ->
  http_request "GET", url, {}, cb
 
resolve = (result, name, data, cb) ->
  
  r = { }
  
  jade_path = "#{__dirname}/../../../../gfportalview/precompile/#{data.jade}"
  delete data.jade
  tasks = for own key,url of data
          do (key,url) ->
            (task_cb) ->
              http_request2 url, (err, data) ->
                if err
                  r[key] = null
                else
                  r[key] = data
                task_cb(null)
  async.parallel tasks, (err) ->
    template = fs.readFileSync(jade_path)
    result[name] = jade.compile(template,{filename:jade_path})(r)
    cb(null)

exports.render_html = (req, res) ->
  
  info = req.param("info")
  
  unless info
    return resErr res, "调用参数错误"
    
  for k,v of info
    unless typeof(v.jade) == "string"
      return resErr res,"调用参数格式错误: 必须带 jade, 指定模版"
    for own k2,v2 of v
      if k2 == "jade"
        continue          
      else
        unless v2.indexOf("://store.gf.com.cn") >= 0
          return resErr res, "调用参数格式错误: 只能调用http://store.gf.com.cn的API: #{{k2,v2}}"
        if unescape(v2).indexOf("/render/html") >= 0
          return resErr res, "调用参数格式错误: 不能递归调用: #{{k2,v2}}"

  result = { }

  tasks = for k,v of info
            do (k,v) ->
              (task_cb) ->
                resolve(result, k,v,task_cb)
                
  async.parallel tasks, (err) ->
    if not err
      return res.json result
    else
      return resErr res,err
  

resolve_page = (page) ->
  
  if page not of ds
    return cb "没有这个页面"

  jade_path = "#{__dirname}/../../../views/gfportalview/partials/#{page}.jade"
  template = fs.readFileSync(jade_path)
  
  if typeof(ds[page]) == "object"
    r = {}
    get_one = ([key,url]) ->
      Q.nfcall(http_request2,url)
        .then (data) ->
          r[key] = data
          
    series(_.pairs(ds[page]), get_one)
      .then ->
        return jade.compile(template,{filename:jade_path})(r)
  else
    Q.nfcall(http_request2,ds[page])
      .then (data) ->
        return jade.compile(template,{filename:jade_path})(data)

resolve_page_helper = (page,recover_error=t) ->
  resolve_page(page)
    .catch (err) ->
      if recover_error
        contingency_page = "#{__dirname}/../data/contingency/#{page}.html"
        return fs.readFileSync(contingency_page).toString()
      else
        throw err
            
exports.render_precompile = (req,res) ->
  pages = req.param("page")
  dont_recover = req.param("dont_recover")
  
  if not pages
    return resErr res, "缺少必选的page参数"
  pages = pages.split(',')
  
  Q.all(resolve_page_helper(page,not dont_recover) for page in pages)
    .then (results) ->
      if results.length == 1
        return res.send results[0]
      else
        return res.json _.object(pages,results)
    .catch fail(res)



  

      
    

      

